package g4p.newg4p;

import g4p_controls.G4P;
import g4p_controls.GEditableTextControl;
import g4p_controls.GEvent;
import g4p_controls.GTextArea;
import processing.core.PApplet;

public class TextAreaInsert_Test extends PApplet {

	
	GTextArea txa0, txa1;
	int[] cpos = new int[2];
	
	public void setup(){
		size(800,300);
		
		String st0 = "Create a text area with both horizontal and \nvertical scrollbars that automatically hide\nwhen not needed.\n";
		String st1 = "Create a text area with both horizontal and vertical scrollbars that automatically hide when not needed.\n";
		
		
		// Create a text area with both horizontal and 
		// vertical scrollbars that automatically hide 
		// when not needed.
		txa0 = new GTextArea(this, 40, 20, 290, 150, G4P.SCROLLBARS_BOTH ); //| G4P.SCROLLBARS_AUTOHIDE);
		txa0.setText(st0);
		txa0.tag = "3 line text";
		txa1 = new GTextArea(this, 440, 20, 290, 150, G4P.SCROLLBARS_BOTH ); //| G4P.SCROLLBARS_AUTOHIDE);
		txa1.setText(st1);
		txa1.tag = "paragraph text";
		println(txa0.getCaretPos());
		println(txa1.getCaretPos());
		
	}
	
	
	public void handleTextEvents(GEditableTextControl etc, GEvent event) {
		GTextArea c = (GTextArea) etc;
		if(event == GEvent.CHANGED){
			System.out.println("==============================================================");
			System.out.println("\t" + c.tag);
			System.out.println("\t" + c.getStyledText().getNbrLines());
			System.out.println("==============================================================");			
		}
	}
 
	public void keyTyped(){
		int end = 15, len;
		switch(key){
		case '1':
			txa0.insertText(" lives in Wales", 2, end, false, false);
			txa0.insertText("Peter Lager", 2, end, false, false);
			details(txa0);
			break;
		case '2':
			txa1.insertText(" lives in Wales", 2, end, false, false);
			txa1.insertText("Peter Lager", 2, end, false, false);
			details(txa1);
			break;
		case '3':
			txa0.insertText(" lives in Wales", 0, 0, false, true);
			txa0.insertText("Peter Lager", 0, 0, true, false);
			details(txa0);
			break;
		case '4':
			txa1.insertText(" lives in Wales", 0, 0, false, true);
			txa1.insertText("Peter Lager", 0, 0, true, false);
			details(txa1);
			break;
		case '5':
			txa0.insertText(" lives in Wales", 0, 0);
			txa0.insertText("Peter Lager", 0, 0);
			details(txa0);
			break;
		case '6':
			txa1.insertText(" lives in Wales", 0, 0);
			txa1.insertText("Peter Lager", 0, 0);
			details(txa1);
			break;
		case '7':
			len = txa0.getTextLength(0, true);
			txa0.insertText("X", 0, len);
			details(txa0);
			break;
		case '8':
			len = txa1.getTextLength(0, true);
			System.out.println("Length " + len);
			txa1.insertText("X ", 0, len);
			details(txa1);
			break;
		case 'a':
			txa0.insertText(" peter lager ", 1, 8);
			txa1.insertText(" peter lager ", 1, 8);
			break;
		case 'b':
			txa0.insertText(" lives in Rhyl ", true, true);
			txa1.insertText(" lives in Rhyl ", true, true);
			break;
		case 'c':
			txa0.insertText(" North Wales ", true, true);
			txa1.insertText(" North Wales ", true, true);
			break;
		case 'd':
			txa0.insertText(" peter lager ", 1, 8);
			txa1.insertText(" peter lager ", 1, 8);
			break;
		case 'e':
			txa0.insertText(" lives in Rhyl ", true, false);
			txa1.insertText(" lives in Rhyl ", true, false);
			txa1.getCaretPos(cpos);
			System.out.println(cpos[0] + ", " + cpos[1]);
			break;
		case 'f':
			txa0.insertText(" North Wales ", false, true);
			txa1.insertText(" North Wales ", false, true);
			break;
		}
	}
	
	public void details(GTextArea c){
		System.out.println("==============================================================");
		System.out.println("\t" + c.tag);
		System.out.println("\t" + c.getStyledText().getNbrLines());
		System.out.println("==============================================================");			

	}
	public void draw(){
		background(200);
	}
}
